//
//  ControlFlowViewController.swift
//  SwiftTest
//
//  Created by midea on 16/3/11.
//  Copyright © 2016年 Vincent. All rights reserved.
//

import Foundation
import UIKit

class ControlFlowViewController : BaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.titles = ["forFlow","whileFlow","ifFlow","switchFlow","tupleFlow"]
        self.tableView?.reloadData()
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        switch indexPath.row{
        case 0:
            forFlow()
        case 1:
            whileFlow()
        case 2:
            ifFlow()
        case 3:
            switchFlow()
        case 4:
            tupleFlow()
        default:
            print("no more methods")
        }
    }
    
    fileprivate func forFlow() {
        for index in 1...5 {
            print("index is " + String(index))
        }
        
        let array : Array = [1,2,3,4]
        for index in array {
            print("array subobject is " + String(index))
        }
        
        let dic : Dictionary = ["1":1,"2":false,"3":["3","4"]] as [String : Any]
        for (key,value) in dic {
            print("key is " + key)
            print("value is " + String(describing: value))
        }
        
//        for i in 0 ..< 5 += 1 {
//            
//        }
    }
    
    fileprivate func whileFlow() {
        var int = 0
        while int < 5 {
            print("int is " + String(int))
            int += 1
        }
    }
    
    fileprivate func ifFlow() {
        let boolean = true
        if boolean {
            print("this is if flow")
        }
        
        let 条件 = 1
        if 条件 == 0 {
            print("条件 is " + String(条件))
        }else if 条件 == 1 {
            print("条件 is " + String(条件))
        }else {
            print("条件 is " + String(条件))
        }
        
        let op1: String? = "Hello"
        let op2: String? = "Swift"
        if let hello = op1, op1 == "Hello" || hello.hasSuffix("o"),
            let name = op2, name.hasPrefix("S") && hello.hasSuffix("o") {
                
        }else
        {
            
        }
    }
    
    fileprivate func switchFlow() {
        let string = "world"
        switch string {
        case "hello","world":
            print("string is hello or world")
        case "what?":
            print("string is what?")
        default:
            print("string is other words")
        }
    }
    
    fileprivate func tupleFlow() {
        let tuple = (1,1)
        switch tuple {
        case (0,0):
            print("this tuple is (0,0)")
        case (_,_):
            print("this tuple is (_,_)")
        case (_,1):
            print("this tuple is (_,1)")
        case (1,_):
            print("this tuple is (1,_)")
        default:
            print("this tuple is not correct")
        }
    }
}
